<template>
  <component :is="type" v-bind="linkProps()">
    <slot />
  </component>
</template>

<script setup lang="ts">
import {computed} from 'vue';
import {VAR_TAG} from "@/stores/conf";

const props = defineProps({
  to: {
    type: [String, Object],
    required: true
  }
})

const isExternal = (path: any) => {
  return /^(https?:|mailto:|tel:)/.test(path)
}

const isExt = computed(() => {
  return isExternal(props.to)
})

const type = computed(() => {
  if (isExt.value) {
    return 'a'
  }
  return 'router-link'
})

const linkProps = () => {
  const routePath = props.to.replace(VAR_TAG, '');

  if (isExt.value) {
    return {
      href: routePath,
      target: '_blank',
      rel: 'noopener'
    }
  }
  return {
    to: routePath
  }
}
</script>
